package com.getword.service.impl;

import com.getword.domain.ArticleCollect;
import com.getword.domain.User;
import com.getword.mongodao.ArticleCollectRepository;
import com.getword.mongodao.ArticleRepository;
import com.getword.service.CollectService;
import com.getword.utils.SpringContextUtil;
import com.getword.web.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


@Service
public class CollectServiceImlp implements CollectService {
    @Autowired
    private ArticleCollectRepository articleCollectRepository;
    @Autowired
    private ArticleRepository articleRepository;

    @Override
    public Result getArticleCollect(Pageable page, Long gradeId) {
        User user = SpringContextUtil.getLoginUser();
        Page<ArticleCollect> result = null;
        if(gradeId == 0){
            result =  articleCollectRepository.findArticleCollectsByUserIdOrderByCollectTimeDesc(user.getId(), page);
        }else{
            result = articleCollectRepository.findArticleCollectsByUserIdAndGradeIdOrderByCollectTimeDesc(user.getId(), gradeId, page);
        }
        for(ArticleCollect a : result.getContent()){
            a.setArticle(articleRepository.findArticleById(a.getArticleId()));
        }
        return Result.success(result);
    }
}